<?php
class Campania extends Controller
{
	public function __construct()
	{
		parent::Controller();
		$this->load->model(array('campaniamodel','mservicios'));
	}
	
	public function index()
	{
			$this->load->view('main',array('content_page' => 'viewcampania', 'menutop' => 0));	
	}
	
	
	public function data($filtro = false)
	{
		 $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		 $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'IdCampania'; // Columna para el order by
		 $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		 $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;

		 $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );
		   
		
	     $records = $this->campaniamodel->loadData($start, $limit, $sortname, $sortorder);
	    
		 $totalRecords = $this->campaniamodel->getCount();
		
		 $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		       $page = $totalPages;

		    $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false){
			     foreach($records as $objetos){
				  
			     	 $servicios = $this->mservicios->LoadServicioById($objetos->getServicios());
			   
				     $arr = $json_output['rows'][] = array(
				          'IdCampania'              => $objetos->getIdCampania(),
				          'tituloCampania'          => $objetos->getTituloCampania(),
				     	  'servicos'                => $servicios->getNombreServicio(),
				     	  'numeroCampania'          => $objetos->getNumeroOrden()
				          
				      );
			      }
		      }

		 echo json_encode($json_output);
	}
	
	public function add()
	{
		 $this->load->view('main',array('content_page' => 'campania_add','menutop' => 0));
	}
	
    public function edit($id)
	{
		$registro = $this->campaniamodel->loadDataById($id);
   		$this->load->view('main',array('content_page' => 'campania_edit',
   		 									 'menutop' => 0, 
   			 								 'objeto'  => $registro));
	}
	
	public function save()
	{
		$dataArray = array("tituloCampania"      => $this->input->post('titulocamp'),
						   "servicos"			 => $this->input->post('servicio'),
						   "numeroCampania"      => $this->input->post('numeroCamp'));  
   	
   	   if ($this->input->post('IdCampania') == false) {
   	   	   if ($this->campaniamodel->addRegistro($dataArray) == true) {
   	   	   	   $mensaje = "El resgistro se agrego exitosamente.";
   	   	   
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	      
   	   	   }
   	   	  
   	   }else{
   	       if ($this->campaniamodel->editRegistro($dataArray, $this->input->post('IdCampania')) == true) {
   	   	   	   $mensaje = "El resgistro se edito exitosamente";
   	   	  
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	   
   	   	   }
   	   }
   	   
   	   redirect('campania');
	}
	
	public function delete()
	{
		$id = $this->input->post('delete_rows');
   		$this->campaniamodel->deleteRegistro($id);
	}
	
}  